import { reqNew, reqGroupon, reqCountdown, reqSeckill, reqIndexgoods, reqCate, reqCoupon } from "../../request/api"
import { createSelector } from "reselect"
//初始值
const initState = {
    //新人专享
    news: [],
    //万人团
    groupons: [],
    //倒计时
    countdown: [],
    //秒杀
    seckill: [],
    //商品分类
    cateList: [],
    //
    banner: [],
    //首页商品
    goods: [],
    filter: 0,
    //优惠券
    coupon: [],
    id:0,
    type:1,
    num:1

}

//actionTypes
const types = {
    //修改 
    CHANGE_NEWS: "HOME_CHANGE_NEWS",
    CHANGE_GROUPONS: "HOME_CHANGE_GROPONS",
    CHANGE_COUNTDOWN: "HOME_CHANGE_COUNTDOWN",
    CHANGE_SECKILL: "HOME_CHANGE_SECKILL",
    CHANGE_BANNER: "HOME_CHANGE_BANNER",
    CHANGE_GOODS: "HOME_CHANGE_GOODS",
    CAHNGE_FILTER: "HOME_CHANGE_FILTER",
    CHANGE_CATELIST: "HOME_CHANGE_CATELIST",
    CHANGE_COUPON: "HOME_CHANGE_COUPON",
    CHANGE_ID:"HOME_CHANGE_ID"
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_NEWS:
            return {
                ...state,
                news: action.arr
            }
        case types.CHANGE_GROUPONS:
            return {
                ...state,
                groupons: action.arr
            }
        case types.CHANGE_COUNTDOWN:
            return {
                ...state,
                countdown: action.arr
            }
        case types.CHANGE_SECKILL:
            return {
                ...state,
                seckill: action.arr
            }
        case types.CHANGE_GOODS:
            return {
                ...state,
                goods: action.arr
            }
        case types.CAHNGE_FILTER:
            return {
                ...state,
                filter: action.filter
            }
        case types.CHANGE_CATELIST:
            return {
                ...state,
                cateList: action.arr
            }
        case types.CHANGE_COUPON:
            return {
                ...state,
                coupon: action.arr
            }
        case types.CHANGE_ID:
            return {
                ...state,
                id: action.id
            }

        default:
            return state;
    }

}

//actionCreators组件派发任务
export const actions = {
    //修改，请求\
    changeNews: arr => ({ type: types.CHANGE_NEWS, arr }),
    reqNews: history => (dispatch, getState) => {
        reqNew().then(res => {
            if (res.data.code === 200) {
                //修改news----------
                dispatch(actions.changeNews(res.data.list))
            }
        })
    },
    changeGroupons: arr => ({ type: types.CHANGE_GROUPONS, arr }),
    reqGroupon: history => (dispatch, getState) => {
        reqGroupon().then(res => {
            if (res.data.code == 200) {
                dispatch(actions.changeGroupons(res.data.list))
            }
        })
    },
    //倒计时
    changecountdown: arr => ({ type: types.CHANGE_COUNTDOWN, arr }),
    reqCountdown: history => (dispatch, getState) => {
        reqCountdown().then(res => {
            if (res.data.code) {
                dispatch(actions.changecountdown(res.data.list))
            }
        })
    },
    //秒杀
    changeScekill: arr => ({ type: types.CHANGE_SECKILL, arr }),
    reqSeckill: history => (dispatch, getState) => {
        reqSeckill().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeScekill(res.data.list))
            }
        })
    },
    //首页商品
    changegoods: arr => ({ type: types.CHANGE_GOODS, arr }),
    reqIndexgoods: history => (dispatch, getState) => {
        reqIndexgoods().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changegoods(res.data.list))
            }
        })
    },
    changeFilter: filter => ({ type: types.CAHNGE_FILTER, filter }),
    //商品分类
    changeCatelist: arr => ({ type: types.CHANGE_CATELIST, arr }),
    reqCate: history => (dispatch, getState) => {
        reqCate().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeCatelist(res.data.list))
            }
        })
    },
    //优惠券
    changeCoupon: arr => ({ type: types.CHANGE_COUPON, arr }),
    reqCoupon: history => (dispatch, getState) => {
        reqCoupon().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeCoupon(res.data.list))
            }
        })
    },
    changeId:id=>({type:types.CHANGE_ID,id}),
   

}

//selectors 导出数据
export const getNews = state => state.home.news
export const getGroupons = state => state.home.groupons
export const getCountdown = state => state.home.countdown
export const getSeckill = state => state.home.seckill
export const getIndexgoods = state => state.home.goods
export const getFilter = state => state.home.filter
export const getShowGoods = createSelector(
    [getIndexgoods, getFilter],
    (goods, filter) => {
        return goods.length > 0 ? goods[filter].content : []
    }
)
export const getCatelist = state => state.home.cateList
export const getCoupon = state => state.home.coupon
export const getId = state => state.home.id
export const getShowCoupon = createSelector(
    [getCoupon, getId],
    (coupon, id) => {
        return coupon.length > 0 ? coupon[id].content : []
    }
)
export default reducer